package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.common.enums.inOutStock.InOutStockTypeEnum;
import com.canaan.business.common.enums.storehouse.InOutStorageOperationEnum;
import com.canaan.business.domain.storehouse.WmsBarcodeRecord;
import com.canaan.business.domain.storehouse.WmsInOutStorage;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.mapper.basic.WmsProductMapper;
import com.canaan.business.mapper.storehouse.WmsInOutStorageMapper;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import com.sun.jna.platform.win32.WinDef;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.ReplacePackMapper;
import com.canaan.business.domain.storehouse.ReplacePack;
import com.canaan.business.service.storehouse.IReplacePackService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@Service
public class ReplacePackServiceImpl implements IReplacePackService {
    @Resource
    private ReplacePackMapper replacePackMapper;

    @Resource
    private WmsBarcodeRecordServiceServiceImpl barcodeRecordServiceService;

    @Resource
    private WmsStorehouseStockServiceImpl storehouseStockService;

    @Resource
    private WmsInOutStorageMapper inOutStorageMapper;

    @Resource
    private WmsProductMapper productMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ReplacePack selectReplacePackById(Long id) {
        return replacePackMapper.selectReplacePackById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param replacePack 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ReplacePack> selectReplacePackList(ReplacePack replacePack) {
        return replacePackMapper.selectReplacePackList(replacePack);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param replacePack 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertReplacePack(ReplacePack replacePack) {

        if(StringUtils.isBlank(replacePack.getNo())){
            replacePack.setNo(TableNoUtil.getNo(ReplacePack.class));
        }

        WmsStorehouseStock storehouseStock = storehouseStockService.getInfoByBarcode(replacePack.getSourceBoxBarcode());

        if(storehouseStock.getQty() < replacePack.getAfterBoxNum()){
            throw new ServiceException("拆箱数量不能大于原箱数量！");
        }

        WmsBarcodeRecord barcodeRecord = new WmsBarcodeRecord();
        barcodeRecord.setSupplierName(storehouseStock.getSupplierName());
        barcodeRecord.setProductionDate(storehouseStock.getProductionDate());
        barcodeRecord.setEffectiveDate(storehouseStock.getEffectiveDate());
        barcodeRecord.setValidDateNum(storehouseStock.getValidDateNum());
        barcodeRecord.setBatchNo(storehouseStock.getBatchNo());
        barcodeRecord.setOrderNo(storehouseStock.getOrderNo());
        barcodeRecord.setProductName(replacePack.getProductName());
        barcodeRecord.setProductNo(replacePack.getProductNo());
        barcodeRecord.setQuantity(replacePack.getAfterBoxNum());
        barcodeRecordServiceService.insertWmsBarcodeRecord(barcodeRecord);

        storehouseStock.setQty(storehouseStock.getQty()-replacePack.getAfterBoxNum());
        if(storehouseStock.getQty() <= 0) {
            storehouseStock.setDelFlag("1");
        }
        storehouseStockService.updateWmsStorehouseStock(storehouseStock);

        WmsInOutStorage inOutStorage = new WmsInOutStorage();
        inOutStorage.setCreateBy(SecurityUtils.getUsername());
        inOutStorage.setCreateTime(DateUtils.getNowDate());
        inOutStorage.setAreaNo(storehouseStock.getStorehouseAreaNo());
        inOutStorage.setBatchNo(storehouseStock.getBatchNo());
        inOutStorage.setOperation(InOutStorageOperationEnum.OUT.getCode());
        inOutStorage.setProductName(barcodeRecord.getProductName());
        inOutStorage.setProductNo(barcodeRecord.getProductNo());
        inOutStorage.setProductUnit(storehouseStock.getCompany());
        inOutStorage.setQuantity(replacePack.getAfterBoxNum());
        inOutStorage.setRelNo(replacePack.getNo());
        inOutStorage.setStorehouseLocationNo(storehouseStock.getStorehouseLocationNo());
        inOutStorage.setStorehouseNo(storehouseStock.getStorehouseNo());
        inOutStorage.setType(InOutStockTypeEnum.REPLACE.getCode().longValue());
        inOutStorage.setNo(TableNoUtil.getNo(WmsInOutStorage.class));
        inOutStorage.setBarcode(storehouseStock.getBarcode());
        inOutStorageMapper.insertWmsInOutStorage(inOutStorage);
        inOutStorage.setOperation(InOutStorageOperationEnum.IN.getCode());
        inOutStorage.setNo(TableNoUtil.getNo(WmsInOutStorage.class));
        inOutStorage.setBarcode(barcodeRecord.getBarCode());
        inOutStorageMapper.insertWmsInOutStorage(inOutStorage);

        WmsStorehouseStock stock = new WmsStorehouseStock();
        BeanUtils.copyProperties(storehouseStock, stock);
        stock.setBarcode(barcodeRecord.getBarCode());
        stock.setQty(replacePack.getAfterBoxNum());
        storehouseStockService.insertWmsStorehouseStock(stock);

        replacePack.setNewBarcode(barcodeRecord.getBarCode());
        replacePack.setCreateBy(SecurityUtils.getUsername());
        replacePack.setCreateTime(DateUtils.getNowDate());
        return replacePackMapper.insertReplacePack(replacePack);
    }

    /**
     * 批量新增【请填写功能名称】
     *
     * @param replacePackList 【请填写功能名称】列表
     * @return 结果
     */
    @Override
    public int batchInsertReplacePack(List<ReplacePack> replacePackList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReplacePack replacePack : replacePackList){
            if(StringUtils.isBlank(replacePack.getNo())){
                replacePack.setNo(TableNoUtil.getNo(ReplacePack.class));
            }
            replacePack.setCreateBy(username);
            replacePack.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<replacePackList.size();) {
            int endIndex = i+500;
            if (endIndex > replacePackList.size()) {
                endIndex = replacePackList.size();
            }
            insertNum = insertNum + replacePackMapper.batchInsertReplacePack(replacePackList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param replacePack 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateReplacePack(ReplacePack replacePack) {
        replacePack.setUpdateBy(SecurityUtils.getUsername());
        replacePack.setUpdateTime(DateUtils.getNowDate());
        return replacePackMapper.updateReplacePack(replacePack);
    }

    /**
     * 批量修改【请填写功能名称】
     *
     * @param replacePackList 【请填写功能名称】列表
     * @return 结果
     */
    @Override
    public int batchUpdateReplacePack(List<ReplacePack> replacePackList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (ReplacePack replacePack : replacePackList){
            replacePack.setUpdateBy(username);
            replacePack.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<replacePackList.size();) {
            int endIndex = i+500;
            if (endIndex > replacePackList.size()) {
                endIndex = replacePackList.size();
            }
            updateNum = updateNum + replacePackMapper.batchUpdateReplacePack(replacePackList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键集合
     * @return 结果
     */
    @Override
    public int deleteReplacePackByIds(Long[] ids) {
        return replacePackMapper.deleteReplacePackByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteReplacePackById(Long id) {
        return replacePackMapper.deleteReplacePackById(id);
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param no 编号
     * @return 【请填写功能名称】
     */
    @Override
    public ReplacePack selectReplacePackByNo(String no) {
        return replacePackMapper.selectReplacePackByNo(no);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteReplacePackByNos(String[] nos) {
        return replacePackMapper.deleteReplacePackByNos(nos);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteReplacePackByNo(String no) {
        return replacePackMapper.deleteReplacePackByNo(no);
    }
}
